<?php
if (isset($_GET['options_values_id'])){
	$op_ids = array_map('zen_string_to_int',explode('_', $_GET['options_values_id']));
	$from_option = ',sf_products_attributes pa ';
	$join_option = 'and pa.products_id=p.products_id and pa.options_values_id in('.implode(',', $op_ids).') ';
	$group_by = 'group by p.products_id having count(*)>='.count($op_ids).' ';
}else 
	$from_option = $join_option = $group_by = '';
	
if (isset($_GET['alpha_filter_id'])&&!empty($_GET['alpha_filter_id'])){
	$alpha_sort = " and pd.products_name REGEXP '^".chr((int)$_GET['alpha_filter_id']) . "' ";
}else {
	$alpha_sort = '';
}			
$sql = 'select p.products_image, pd.products_name, p.products_id,p.products_type, p.products_price, p.products_tax_class_id from '. 
	   TABLE_PRODUCTS.' p , '.TABLE_PRODUCTS_TO_CATEGORIES.' p2c ,'.TABLE_PRODUCTS_DESCRIPTION.' pd '.$from_option.
	   'where p.products_status = 1 and p.products_id = p2c.products_id '.
	   'and pd.products_id = p2c.products_id and pd.language_id ='.(int)$_SESSION['languages_id'].' '.
	   'and p2c.categories_id IN (%s) '.
	   $join_option.
	   $alpha_sort.
	   $group_by.
	   'order by p.products_sort_order, pd.products_name,p.products_id';
$sql = 'select * from ('.$sql.') as t';	   
$c = array_map('zen_string_to_int',explode('_', $_GET['cPath']));	
if (empty($c)){
	$c = 0;
}else{
	$c = array_pop($c);
}
$p2c_categories_id = array();
zen_get_subcategories($p2c_categories_id,$c);
$p2c_categories_id[] = $c;//分类
$sql = sprintf($sql,implode(',', $p2c_categories_id));
//echo $sql;exit;
$r = $db->Execute($sql);
$r_show = array();
$row_index = 1;
while (!$r->EOF){
	if ($r->fields['products_id']==$_GET['products_id'])
		break;
	$row_index++;
	$r->MoveNext();
}
$count_sql = 'select count(*) as count from('.$sql.') as t1';
$r = $db->Execute($count_sql);
$total = $r->fields['count'];//总数量
$num_perpage = 8;//每页显示8条
$page_num = ceil($total/$num_perpage);//总页数
$cur_page = isset($_GET['ajax_page'])?(int)$_GET['ajax_page']:(ceil($row_index/$num_perpage));//当前页码
$cur_page = $cur_page>$page_num?$page_num:($cur_page<0?1:$cur_page);
//	$sql_show = $sql.' limit '.(($cur_page-1)*8).','.$num_perpage;
?>
<div class="navNextPrevWrapper centeredContent">
	<div class="navNextPrevWrapperTitle">Page Navigation(<span id="navNextPrevWrapperCurPage"><?php echo $cur_page?></span>/<span id="navNextPrevWrapperTotalPage"><?php echo $page_num?></span>)</div>
	<div id="navNextPrevWrapperContent">
		<div id="navNextPrevWrapperContent-back"></div>
		<div id="navNextPrevWrapperContent_show"></div>
		<div id="navNextPrevWrapperContent-next"></div>
	</div>
</div>
<script>
<?php 
$data = 'ajax=true&product_info=true&products_id='.$_GET['products_id'];
if (isset($_GET['alpha_filter_id'])&&!empty($_GET['alpha_filter_id'])){
	$data .= '&alpha_filter_id='.$_GET['alpha_filter_id'];
}
if (isset($_GET['options_values_id'])){
	$data .= '&options_values_id='.$_GET['options_values_id'];
}	
if (isset($_GET['cPath'])){
	$data .= '&cPath='.$_GET['cPath'];	
}
?>
function loading(){
	$('#navNextPrevWrapperContent_show_loading').append('.');
}
function TurnToPage(page){
	if(!page) page = 1;
	var LOADING_TIMER;
	$("#navNextPrevWrapperContent_show").ajaxStart(function(){
		$('#navNextPrevWrapperContent_show').html("");
		 $('<div id="navNextPrevWrapperContent_show_loading" style="text-align:center;color:red;font-size:20px;">Loading</div>').appendTo('#navNextPrevWrapperContent_show');
		 LOADING_TIMER = setInterval('loading()',1000);//每隔1秒加个点
	}).ajaxStop(function(){
		 clearInterval(LOADING_TIMER);
	});
	$.ajax({
		   type: "GET",
		   url: "<?php echo zen_href_link('ajax') ?>",
		   data: "<?php echo $data?>"+"&ajax_page="+page,
		   dataType:"json",
		   success: function(data){
			   $('#navNextPrevWrapperContent_show').html("");
			   $.each(data, function(i, entry){
				    var item = $('<div class="item"></div>').appendTo('#navNextPrevWrapperContent_show');
				    item.append('<a href="'+entry.href+'" class="item_img">'+entry.special_flag+'<img src="'+entry.products_image+'" alt="'+entry.products_name+'" width="100px" height="100px"></a>')
				        .append('<br />')
				        .append('<a href="'+entry.href+'" class="item_name">'+entry.products_name+'</a>')
				        .append('<br />')
				        .append('<div class="item_price">'+entry.products_price+'</div>');
			        if(entry.products_id==<?php echo $_GET['products_id']?>)
			        	item.addClass('cur_product');
			   });
		   },
		   error:function(error){
//				alert(error);
		   }
	});
	
}
$(document).ready(function(){
	TurnToPage(<?php echo $cur_page?>);
});
$('#navNextPrevWrapperContent-next').click(function(){
	var navNextPrevWrapperCurPage = parseInt($('#navNextPrevWrapperCurPage').text());
	var navNextPrevWrapperTotalPage = parseInt($('#navNextPrevWrapperTotalPage').text());
	if(navNextPrevWrapperCurPage>=navNextPrevWrapperTotalPage){
		return;
	}else{
		TurnToPage(navNextPrevWrapperCurPage+1);
		$('#navNextPrevWrapperCurPage').text(navNextPrevWrapperCurPage+1);
		
	}
});
$('#navNextPrevWrapperContent-back').click(function(){
	var navNextPrevWrapperCurPage = parseInt($('#navNextPrevWrapperCurPage').text());
	var navNextPrevWrapperTotalPage = parseInt($('#navNextPrevWrapperTotalPage').text());
	if(navNextPrevWrapperCurPage<=1){
		return;
	}else{
		TurnToPage(navNextPrevWrapperCurPage-1);
		$('#navNextPrevWrapperCurPage').text(navNextPrevWrapperCurPage-1);
		
	}
});
</script>